//
//  HACLineChart.m
//  HStockCharts
//
//  Created by Hotacool on 2018/1/4.
//

#import "HACLineChart.h"
#import "HACLineChartView.h"
#import "HACPointEntry.h"

@interface HACLineChart ()
@property (nonatomic, strong) HACLineChartView *lineChartView;
@end

@implementation HACLineChart

- (void)setUp {
    [super setUp];
    self.focusLabelShow = YES;
    self.contentView.bounces = NO;
    
    _lineChartView = [[HACLineChartView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_lineChartView];
}

- (void)startAnimateWithDuration:(NSTimeInterval)duration {
    [self.lineChartView.animator animateXWithDuration:duration];
}

- (HACAxisComponents *)leftAxis {
    return _lineChartView.leftAxis;
}

- (HACAxisComponents *)rightAxis {
    return _lineChartView.rightAxis;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    _lineChartView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

- (void)setData:(HACChartData *)data {
    [super setData:data];
    [self.lineChartView setData:data];
}

- (void)refreshWithData:(HACChartData *)data {
    [super refreshWithData:data];
    [self.lineChartView setData:data];
    [self refreshUI];
}

- (void)refreshUI {
    [self.lineChartView refreshUI];
}

- (void)reset {
    self.data = nil;
    [self.lineChartView clean];
}

- (void)setVisualCount:(NSInteger)visualCount {
    [self.lineChartView setVisualCount:visualCount];
}

#pragma mark - actions
- (void)reactForLongPressGesture:(UILongPressGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self];
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        //偏移，因gesture所在view不随scroll移动
        point = CGPointMake(point.x + self.contentView.contentOffset.x, point.y + self.contentView.contentOffset.y);
        self.focusIndex = [self.lineChartView indexAtChartForCoordinate:point];;
        self.focusPoint = [self.lineChartView pointAtChartForIndex:self.focusIndex];
    } else {
        //保留显示label
//        self.focusIndex = -1;
    }
}

#pragma mark - provider
- (NSArray *)focusDataInRect:(CGRect)rect focusIndex:(NSInteger)focusIndex {
    if (self.focusItemsCount == 0 || focusIndex >= self.lineChartView.actualCount || focusIndex == -1)
        return nil;
    
    NSMutableArray *focusDrawingData = [NSMutableArray array];
    CGFloat x = 0.0, y = 0.0;
    for (NSInteger i = 0; i < self.focusItemsCount; i++) {
        HACFocusComponents *item = [[HACFocusComponents alloc] init];
        item.index = i;
        item.axisType = i == 0 ? DrawingAxisTypeY : DrawingAxisTypeX; //第一根是竖线，第二根横线
        item.text = [self stringForFocusItem:item focusIndex:focusIndex];
        
        CGSize textSize = [item.text sizeWithAttributes:@{NSFontAttributeName : item.textFont}];
        // DrawingAxisTypeX不显示
        if (item.axisType == DrawingAxisTypeX) {
            textSize = CGSizeZero;
        } else {
            if (textSize.width < 40) {
                textSize.width = 40;
            }
        }
        CGRect frame = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), textSize.width , textSize.height);
        CGLine line = CGLineMake(0, 0, 0, 0);
        
        CGPoint focusPoint = [self.lineChartView pointAtChartForIndex:focusIndex];
        focusPoint = CGPointMake(focusPoint.x, focusPoint.y);
        
        if (item.axisType == DrawingAxisTypeX) {
            y = focusPoint.y;
            line = CGLineMake(CGRectGetMinX(rect) + textSize.width, y, CGRectGetMaxX(rect), y);
            frame.origin.y = MIN(CGRectGetMaxY(rect), MAX(CGRectGetMinY(rect), y - textSize.height / 2));
        } else {
            x = focusPoint.x;
            line = CGLineMake(x, CGRectGetMinY(rect), x, CGRectGetMaxY(rect));
            frame.origin.x = MIN(CGRectGetMaxX(rect) - textSize.width, MAX(CGRectGetMinX(rect), x - textSize.width / 2));
//            frame.origin.y = CGRectGetMaxY(rect);
            CGFloat marginY = 4;
            if (focusPoint.y + marginY + textSize.height >= CGRectGetMaxY(rect)) {
                frame.origin.y = CGRectGetMaxY(rect) - marginY - textSize.height;
            } else {
                frame.origin.y = focusPoint.y + marginY;
            }
        }
        if (self.focusLabelShow) {
            item.textFrame = frame;
            item.textAlignment = NSTextAlignmentCenter;
            item.textColor = [UIColor whiteColor];
        }
        item.fillColor = [UIColor redColor];
        item.line = line;
        item.point = focusPoint;
        item.lineColor = self.focusComponent.lineColor?:item.lineColor;
        item.lineBolder = self.focusComponent.lineBolder?:item.lineBolder;
        item.borderWidth = self.focusComponent.borderWidth?:item.borderWidth;
        item.bgColor = self.focusComponent.bgColor?:item.bgColor;
        item.borderColor = self.focusComponent.bgColor?:item.bgColor;
        item.textFont = self.focusComponent.textFont?:item.textFont;
        item.textAlignment = self.focusComponent.textAlignment?:item.textAlignment;
        [focusDrawingData addObject:item];
    }
    return focusDrawingData;
}

- (NSString *)stringForFocusItem:(HACFocusComponents *)focusItem focusIndex:(NSInteger)focusIndex {
    NSString *focusString;
    HACPointEntry *data = [self.lineChartView dataAtChartForIndex:focusIndex];
    if (focusItem.axisType == DrawingAxisTypeX) {
        focusString = @(data.x).stringValue;
    } else {
        focusString = @(data.y).stringValue;
    }
    return focusString;
}
@end
